package parsers.teacher;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import parsers.CommonMethods;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import model.teacher.Department;

public class DepartmentsParser
{	
	public DepartmentsParser() {}
	
	public ArrayList<Department> parse(URL url) throws IOException
	{
		ArrayList<Department> departments = new ArrayList<Department>();
		
		Source source = new Source(url);		
		List<Element> elements = source.getAllElements(HTMLElementName.TD);
		
		for (Element element : elements)
		{
			String name = element.toString();
			if(name.contains("Wydział"))
			{
				List<Element> linkElements = element.getAllElements(HTMLElementName.A);
				for (Element linkElement : linkElements) 
				{
					String link = linkElement.getAttributeValue("href");
					if (link==null) 
						continue;
					
					String departmentName = linkElement.getContent().getTextExtractor().toString();
					Department department = new Department(departmentName);
					department.setLink(CommonMethods.parseLink(link));

					departments.add(department);
				}
			}
		}

		return departments;
	}
	
	/*public static void main(String[] args) throws MalformedURLException, IOException
	{
		DepartmentsParser dtp = new DepartmentsParser();
		ArrayList<Department> list = dtp.parse(new URL("file:/D:/test4.html"));
		System.out.println(list);
	}*/
}
